package com.neu.utils.excel.processor;

import com.neu.entity.Project;
import com.neu.entity.security.LoginUser;
import com.neu.utils.excel.DataProcessor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Component
public class ProjectDataProcessor implements DataProcessor<Project> {

    @Override
    public void process(List<Project> projectList) {
        // 数据验证可以放在 DataProcessor 中或在此处进行
        boolean isValid = validateProjectData(projectList);
        if (!isValid) {
            log.error("数据验证失败");
            // 可以选择抛出自定义异常以终止导入过程
            //throw new BusinessException("数据验证失败");
            return;
        }
        LoginUser loginUser = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        // 实现数据处理逻辑，例如清洗、转换等
        for (Project project : projectList) {
            // 导入的项目默认为和当前登录用户同一租户
            project.setTenantId(loginUser.getTenantId());
            project.setDelFlag("1");
            project.setCreateId(loginUser.getUsername());
            project.setUpdateId(loginUser.getUsername());
            project.setCreateTime(LocalDateTime.now());
            project.setUpdateTime(LocalDateTime.now());
        }
    }
    private boolean validateProjectData(List<Project> projectList) {
        for (Project project : projectList) {
            if (!StringUtils.hasText(project.getProjectName())) {
                log.warn("项目名为空: " + project);
                return false;
            }
        }
        return true;
    }
}
